#include "webview.h"
#include "ui_webview.h"

#include <QLayout>
#include <QWebFrame>
#include <QWebElement>

#include <QDebug>

WebView::WebView(QWidget *parent) :
    QWebView(parent),
    ui(new Ui::WebView)
{
    ui->setupUi(this);
    this->browserControl = new BrowserControl();

//    this->load(QUrl("https://bangumi.bilibili.com/anime/6325/play#113478"));
    this->load(QUrl("file:///E:/Qt4Project/Browser/web/index.html"));

    //将webview设置成使用flash插件,否则无法加载flash,NPSWF32.dll文件放在plugins文件夹中
    QWebSettings *settings = QWebSettings::globalSettings();
    settings->setAttribute(QWebSettings::PluginsEnabled, true);//
    settings->setAttribute(QWebSettings::JavascriptEnabled, true);//JavaScript
    settings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);//
    settings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
    settings->setAttribute(QWebSettings::LocalStorageEnabled, true);
    settings->setAttribute(QWebSettings::AutoLoadImages, true);
    settings->setAttribute(QWebSettings::LinksIncludedInFocusChain, true);

    //将页面的显示的的滚动条禁止
//    this->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
//    this->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlwaysOff);
//    this->setContextMenuPolicy(Qt::NoContextMenu);

/*
    因为在发布的时候添加了外部的插件，所以在main.cpp中添加了如下代码
    QApplication::addLibraryPath("./plugins");
        QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
        QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
        QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));

    注:要讲flash player 中的NPSWF32.dll 放置到plugins中
*/

    //设置页面的背景透明
//    this->setStyleSheet("background:transparent");
//    this->setAttribute(Qt::WA_TranslucentBackground);
//    QPalette palette = this->palette();
//    palette.setBrush(QPalette::Background, Qt::transparent);
//    this->page()->setPalette(palette);
//    this->setAttribute(Qt::WA_OpaquePaintEvent, false);

    //将webview中的页面上的插连接全部激活，以便接受linkClicked信号
    this->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

    //单页面，所有链接在当前页面打开
    connect(this,SIGNAL(linkClicked(QUrl)),this,SLOT(openLinkSlot(QUrl)));
    connect(this->page()->mainFrame(),SIGNAL(javaScriptWindowObjectCleared()),this, SLOT(populateJavaScriptWindowObject()));

    QWidget::installEventFilter(this);

    this->setRenderHints(QPainter::HighQualityAntialiasing);
}

WebView::~WebView()
{
    delete ui;
    delete this->browserControl;
}

void WebView::setMainWindow(MainWindow *widget)
{
    this->mainWindow = widget;
    this->browserControl->mainWindow = widget;
    this->browserControl->webView = this;
}

void WebView::populateJavaScriptWindowObject()
{
    this->page()->mainFrame()->addToJavaScriptWindowObject("browserControl", this->browserControl);
}

void WebView::openLinkSlot(QUrl url)
{
    this->load(url);
}

void WebView::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        foreach(QWebElement element, this->page()->mainFrame()->findAllElements(".moveControl")) {
            //判断点击的元素是否可以移动浏览器
            this->isPress = element.geometry().contains(mapTo(this, event->pos()));

            this->mainPosition = this->mainWindow->pos();
            this->pressPosition = mapToGlobal(event->pos());

            if(this->isPress) {
                break;
            }
        }
    }
    QWebView::mousePressEvent(event);
}

void WebView::mouseReleaseEvent(QMouseEvent *event)
{
    this->isPress = false;
    QWebView::mouseReleaseEvent(event);
}

void WebView::mouseMoveEvent(QMouseEvent *event)
{
    if(this->mainWindow->isMaximized()) {
        return;
    }

    if(this->isPress) {
        QPoint movePosition = mapToGlobal(event->pos());
        this->mainWindow->move(this->mainPosition + movePosition - this->pressPosition);
    }

    QWebView::mouseMoveEvent(event);
}

bool WebView::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this) {
        if(QEvent::WindowActivate == event->type()) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
